package org.jeecg.modules.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demand.entity.Demand;
import org.jeecg.modules.demand.entity.DemandTree;
import org.jeecg.modules.demand.service.IDemandService;
import org.jeecg.modules.project.entity.Project;
import org.jeecg.modules.project.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.jeecg.modules.demand.controller.DemandController.DemandToTree;

/**
 * @Description: 项目
 * @Author: jeecg-boot
 * @Date:   2023-09-07
 * @Version: V1.0
 */
@Api(tags="项目")
@RestController
@RequestMapping("/project/project")
@Slf4j
public class ProjectController extends JeecgController<Project, IProjectService> {
	@Autowired
	private IProjectService projectService;
	@Autowired
	private IDemandService demandService;
	
	/**
	 * 分页列表查询
	 *
	 * @param project
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "项目-分页列表查询")
	@ApiOperation(value="项目-分页列表查询", notes="项目-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Project project,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Project> queryWrapper = QueryGenerator.initQueryWrapper(project, req.getParameterMap());
		Page<Project> page = new Page<Project>(pageNo, pageSize);
		IPage<Project> pageList = projectService.page(page, queryWrapper);
		List<String> ids = new ArrayList<>();
		for (Project p : pageList.getRecords()) {
			String id = p.getId();
			ids.add(id);
		}
		QueryWrapper<Demand> wrapper = new QueryWrapper<>();
		wrapper.in("project_id", ids);
		List<Demand> list = demandService.list(wrapper);
		for (Project p : pageList.getRecords()) {
			List<Demand> demandByProjectId = new ArrayList<>();
			for (Demand demand : list){
				if (p.getId().equals(demand.getProjectId())){
					demandByProjectId.add(demand);
				}
			}
			List<DemandTree> demandTrees = DemandToTree(demandByProjectId);
			List<DemandTree> d = new ArrayList<>();
			List<DemandTree> endNode = getEndNode(demandTrees, d);
			double v = calculateCompletionPercentage(endNode);
			p.setProgress((int)v);
		}
		return Result.OK(pageList);
	}

	private List<DemandTree> getEndNode(List<DemandTree> demandTrees, List<DemandTree> d){
		for (DemandTree demandTree : demandTrees){
			if (demandTree.getChildren() == null || demandTree.getChildren().size() == 0){
				d.add(demandTree);
			} else {
				getEndNode(demandTree.getChildren(), d);
			}
		}
		return d;
	}
	public static double calculateCompletionPercentage(List<DemandTree> demands) {
		int totalDemands = demands.size();
		int completedDemands = 0;

		for (DemandTree demand : demands) {
			if (demand.getFinish()) {
				completedDemands++;
			}
		}

		return (completedDemands / (double) totalDemands) * 100;
	}
	
	/**
	 *   添加
	 *
	 * @param project
	 * @return
	 */
	@AutoLog(value = "项目-添加")
	@ApiOperation(value="项目-添加", notes="项目-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Project project) {
		projectService.save(project);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param project
	 * @return
	 */
	@AutoLog(value = "项目-编辑")
	@ApiOperation(value="项目-编辑", notes="项目-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Project project) {
		projectService.updateById(project);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目-通过id删除")
	@ApiOperation(value="项目-通过id删除", notes="项目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		projectService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目-批量删除")
	@ApiOperation(value="项目-批量删除", notes="项目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.projectService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目-通过id查询")
	@ApiOperation(value="项目-通过id查询", notes="项目-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Project project = projectService.getById(id);
		if(project==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(project);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param project
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Project project) {
        return super.exportXls(request, project, Project.class, "项目");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Project.class);
    }

}
